//
//  QYGWebViewController.m
//  wawaji_ios
//
//  Created by 云+物娱 on 2017/12/11.
//  Copyright © 2017年 yiguo qi. All rights reserved.
//

#import "QYGWebViewController.h"
#import <WebKit/WebKit.h>

@interface QYGWebViewController ()
@property (nonatomic, strong) UIProgressView *progress;
@property (nonatomic, strong) WKWebView *webview;
@end

@implementation QYGWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *urlString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)self.urlStr, (CFStringRef)@"!NULL,'()*+,-./:;=?@_~%#[]", NULL, kCFStringEncodingUTF8));
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
    [self.webview loadRequest:request];
    [self.view addSubview:self.webview];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    
    
    [self.webview addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
    [self.webview addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:NULL];
}

- (void)back{
    if(self.webview.canGoBack){
        [self.webview goBack];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}
#pragma mark KVO的监听代理
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    
    //加载进度值
    if ([keyPath isEqualToString:@"estimatedProgress"])
    {
        if (object == self.webview)
        {
            [self.progress setAlpha:1.0f];
            [self.progress setProgress:self.webview.estimatedProgress animated:YES];
            if(self.webview.estimatedProgress >= 1.0f)
            {
                [UIView animateWithDuration:0.5f
                                      delay:0.3f
                                    options:UIViewAnimationOptionCurveEaseOut
                                 animations:^{
                                     [self.progress setAlpha:0.0f];
                                 }
                                 completion:^(BOOL finished) {
                                     [self.progress setProgress:0.0f animated:NO];
                                 }];
            }
        }
        else
        {
            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
        }
    }
    //网页title
    else
        if ([keyPath isEqualToString:@"title"])
        {
            if (object == self.webview)
            {
                self.title = self.webview.title;
            }
            else
            {
                [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
            }
        }
        else
        {
            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
        }
}

#pragma mark 移除观察者
- (void)dealloc
{
    [self.webview removeObserver:self forKeyPath:@"title"];
    [self.webview removeObserver:self forKeyPath:@"estimatedProgress"];
}

#pragma mark - UIWebViewDelegate
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    //    NSLog(@"信息加载有误-----------%@",error);
    [self.view makeToast:@"信息加载有误"];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
}


#pragma mark lazy
- (WKWebView *)webview{
    if(!_webview){
        _webview = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height - self.navigationController.navigationBar.height - STATUSBAR_HEIGHT)];
    }
    return _webview;
}
#pragma mark 加载进度条
- (UIProgressView *)progress
{
    if (_progress == nil)
    {
        _progress = [[UIProgressView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 2)];
        _progress.tintColor = [UIColor blueColor];
        _progress.backgroundColor = [UIColor lightGrayColor];
        [self.view addSubview:_progress];
    }
    return _progress;
}

@end


